﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plat.WfProcess.Common;
using Plat.WfProcess.DataModel;

namespace Plat.WfProcess.Business
{
    /// <summary>
    /// 节点转移管理类
    /// </summary>
    internal class TransitionInstanceManager
    {
        #region ActivityInstanceManager 属性和构造函数
        private TransitionInstanceRepository _transitionInstanceRepository;
        private TransitionInstanceRepository transitionInstanceRepository
        {
            get
            {
                if (_transitionInstanceRepository == null)
                {
                    _transitionInstanceRepository = new TransitionInstanceRepository();
                }
                return _transitionInstanceRepository;
            }
        }
        #endregion

        internal TransitionInstanceEntity CreateTransitionInstanceObject(ProcessInstanceEntity processInstance,
            TransitionEntity transition,
            string fromActivityInstanceGUID,
            string toActivityInstanceGUID,
            byte conditionParseResult)
        {
            TransitionInstanceEntity entity = new TransitionInstanceEntity();
            entity.TransitionInstanceGUID = Guid.NewGuid().ToString();
            entity.ProcessGUID = processInstance.ProcessGUID;
            entity.ProcessInstanceGUID = processInstance.ProcessInstanceGUID;
            entity.TransitionGUID = transition.TranstiontGUID;
            entity.FromActivityInstanceGUID = fromActivityInstanceGUID;
            entity.FromActivityGUID = transition.FromActivityGUID;
            entity.ToActivityInstanceGUID = toActivityInstanceGUID;
            entity.ToActivityGUID = transition.ToActivityGUID;
            entity.ConditionParseResult = conditionParseResult;
            return entity;
        }

        #region 数据增删改查
        internal TransitionInstanceEntity Select(string transitionInstanceGUID)
        {
            return transitionInstanceRepository.SelectById(transitionInstanceGUID);
        }

        internal string GetToActivityInstanceGUID(string fromActivityInstanceGUID)
        {
            string where = "FromActivityInstanceGUID == @0";
            return transitionInstanceRepository.Select(where, fromActivityInstanceGUID)
                .FirstOrDefault()
                .ToActivityInstanceGUID;
        }

        internal string Insert(TransitionInstanceEntity entity)
        {
            return Insert(entity, null);
        }

        internal string Insert(TransitionInstanceEntity entity,
            ProcessModelDataContext wfLinqDataContext)
        {
            if (wfLinqDataContext != null)
            {
                transitionInstanceRepository.LinqDataContext = wfLinqDataContext;
            }

            return transitionInstanceRepository.Insert(entity).TransitionInstanceGUID.ToString();
        }

        /// <summary>
        /// 删除转移实例
        /// </summary>
        /// <param name="transitionInstanceGUID"></param>
        internal void Delete(string transitionInstanceGUID)
        {
            Delete(transitionInstanceGUID, null);
        }

        /// <summary>
        /// 删除转移实例
        /// </summary>
        /// <param name="transitionInstanceGUID"></param>
        /// <param name="wfLinqDataContext"></param>
        internal void Delete(string transitionInstanceGUID,
            ProcessModelDataContext wfLinqDataContext)
        {
            if (wfLinqDataContext != null)
            {
                transitionInstanceRepository.LinqDataContext = wfLinqDataContext;
            }
            transitionInstanceRepository.Delete(transitionInstanceGUID);
        }
        #endregion

        /// <summary>
        /// 判读定义的Transition是否已经被实例化执行
        /// </summary>
        internal bool IsTransiionInstancedAndConditionParsedOK(string transitionGUID, IList<TransitionInstanceEntity> transitionInstanceList)
        {
            bool isConainedAndCompletedOK = false;
            foreach (TransitionInstanceEntity transitionInstance in transitionInstanceList)
            {
                //判断连线是否被实例化，并且条件是否满足
                if (transitionGUID == transitionInstance.TransitionGUID)
                {
                    if (transitionInstance.ConditionParseResult == (byte)ConditionParseResultEnum.Passed)
                    {
                        isConainedAndCompletedOK = true;
                        break;
                    }
                }
            }
            return isConainedAndCompletedOK;
        }
    }
}
